package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Battery;
import com.tbit.uqbike.object.pojo.dto.BatteryQueryDTO;
import com.tbit.uqbike.object.pojo.dto.BmsBatteryQueryDTO;
import com.tbit.uqbike.object.pojo.vo.BatteryVO;
import com.tbit.uqbike.object.pojo.vo.BmsBatteryVO;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BatteryChargeModelService;
import com.tbit.uqbike.service.business.BatteryService;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.utils.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 电池信息表(Battery)表控制层
 *
 * @author jing
 * @since 2020-12-04 13:49:32
 */
@RestController
@RequestMapping("/battery")
public class BatteryController {
    /**
     * 服务对象
     */
    @Resource
    private BatteryService batteryService;
    @Resource
    private BatteryChargeModelService batteryChargeModelService;

    /**
     * 通过主键查询单条数据
     *
     * @param batteryId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer batteryId) {
        return JsonResult.ok(this.batteryService.queryById(batteryId));
    }

    @PostMapping("/add")
    public JsonResult add(Battery battery) {
        /* 校验 */
        Assert.hasLength(battery.getBatteryNOs(), "ver.batteryNO.notNull");
        Assert.notNull(battery.getBatteryModelId(), "ver.batteryModelId.notNull");
        List<Battery> batteries = new ArrayList<>();
        if (null == batteryChargeModelService.queryById(battery.getBatteryModelId())) {
            throw new BaseException(SystemCodeEnum.BATTERY_NOT_EXIST);
        }
        Arrays.stream(battery.getBatteryNOs().split(",")).forEach(batteryNO -> {
            if (!batteryService.queryByBatteryNO(batteryNO)) {
                Battery battery1 = new Battery();
                battery1.setBatteryNO(batteryNO);
                battery1.setBatteryModelId(battery.getBatteryModelId());
                battery1.setChargeTimes(0);
                battery1.setRideMileage(0);
                battery1.setAddTime(new Date());
                battery1.setUpdateTime(new Date());
                batteries.add(battery1);
            }
        });
        if (batteries.size() > 0) {
            batteryService.insertBatch(batteries);
        }
        return JsonResult.ok();
    }

    @PostMapping("/edit")
    public JsonResult edit(Battery battery) {
        Assert.notNull(battery.getBatteryModelId(), "ver.batteryModelId.notNull");
        Assert.hasLength(battery.getBatteryNO(), "ver.batteryNO.notNull");
        Battery battery1 = batteryService.queryById(battery.getBatteryId());
        if (battery1.getBatteryNO() != battery.getBatteryNO() && batteryService.queryByBatteryNO(battery.getBatteryNO())) {
            throw new BaseException(SystemCodeEnum.BATTERYNO_HAVE_EXIST);
        }
        battery.setUpdateTime(new Date());
        batteryService.updateByBattery(battery);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    public JsonResult del(Battery battery) {
        Assert.notNull(battery.getBatteryId(), "ver.batteryId.notNull");
        batteryService.deleteById(battery.getBatteryId());
        return JsonResult.ok();
    }

    @PostMapping("/queryPage")
    public JsonResult queryPage(Battery battery) {
        battery.setPage(new Page(battery.getPageNO(), battery.getRowCount()));
        PageHelperUtil.initPage(battery.getPage());
        PageInfo pageInfo = new PageInfo(batteryService.queryPage(battery));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }


}